package com.eibus.applicationconnector.dotnet;

import junit.framework.TestCase;
import com.eibus.connector.nom.Connector;
import com.eibus.xml.nom.Node;
import com.eibus.xml.xpath.XPath;

//@Author	: Lejil
//@Class	: DotnetDifferentTypesTest
/**
 *	<br/>Owner:			Lejil
 *	<br/>Description:	Invoke the .NET public Instance & Static Methods for default values, different combinations of types etc.. 
 */
public class DotnetDifferentTypesTestCase extends TestCase
{
	Connector  connector;
	private final String namespace = "http://schemas.cordys.com/1.0/dotnet/RefUnitTest";
	private final String unitNamespace ="http://schemas.cordys.com/1.0/dotnet/UnitTestMethods";
	protected void setUp()
	{
		try
		{
			connector = Connector.getInstance("DotNET Client");
			if (!connector.isOpen())
				connector.open();
		
		}
		catch(Exception exception)
		{
			fail("Could not get instance of Connector. "+exception.getMessage());
		}
	}
	
	protected void tearDown()
	{

	}

	public DotnetDifferentTypesTestCase(String name)
	{
		super(name);
	}
	
	public static void main(String[] args)
	{
		junit.textui.TestRunner.run(DotnetDifferentTypesTestCase.class);
	}

	public  boolean checkforSoapFault(int response) throws Exception
	{
	
		try
		{
			int  faultNode = XPathUtil.getSOAPFault(response);

			if ( faultNode != 0)
			{
				fail(".NET Connector junit Method Failed with SoapFault	:-"+Node.writeToString(faultNode,true));
			}
		}
		catch(Exception ex)
		{
			throw ex;
		}
		return true;
	}
	
	
	public void testMixedTypeMethodsWithInt() //1
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"intMixedArgs");
			Node.createTextElement("intArgs","100",request);
			Node.createTextElement("longArgs","3456012",request);
			Node.createTextElement("string","Cordys",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			 //String result = Find.firstDataWithDefault(response,"?<intMixedArgs><intMixedArgs>","False");
             String result = Node.getDataWithDefault(
                        new XPath(".//intMixedArgs/intMixedArgs").firstMatch(response, null),
                        "Not Found");
			if (!result.equalsIgnoreCase("100"))
			{
				fail("intMixedArgs Expected value is 100, but returned different");
			}
			
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- intMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
			
		}
	}
	
	public void testMixedTypeStaticMethodsWithInt() //2
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"intStaticMixedArgs");
			Node.createTextElement("intArgs","100",request);
			Node.createTextElement("longArgs","3456012",request);
			Node.createTextElement("string","Cordys",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<intStaticMixedArgs><intStaticMixedArgs>","False");
            String result = Node.getDataWithDefault(
                        new XPath(".//intStaticMixedArgs/intStaticMixedArgs").firstMatch(response, null),
                        "Not Found");
			if (!result.equalsIgnoreCase("100"))
			{
				fail("intStaticMixedArgs Expected value is 100, but returned different");
			}
			
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- intMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
			
		}

	}
	
	
	public void testMixedTypeRefMethodsWithInt() //3
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"intRefMixedArgs");
			Node.createTextElement("intArgs","100",request);
			Node.createTextElement("longArgs","3456012",request);
			Node.createTextElement("string","Cordys",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<intRefMixedArgs><intRefMixedArgs>","False");
            String result = Node.getDataWithDefault(
                        new XPath("..//intRefMixedArgs/intRefMixedArgs").firstMatch(response, null),
                        "Not Found");
			if (!result.equalsIgnoreCase("100"))
			{
				fail("intRefMixedArgs Expected value is 100, but returned different");
			}
			
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : intRefMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
			
		}		
		
	}
	public void testMixedTypeRefStaticMethodsWithint() //4
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"intStaticRefMixedArgs");
			Node.createTextElement("intArgs","100",request);
			Node.createTextElement("longArgs","3456012",request);
			Node.createTextElement("string","Cordys",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<intStaticRefMixedArgs><intStaticRefMixedArgs>","False");
            String result = Node.getDataWithDefault(
                    new XPath(".//intStaticRefMixedArgs/intStaticRefMixedArgs").firstMatch(response, null),
                    "Not Found");
			if (!result.equalsIgnoreCase("100"))
			{
				fail("intStaticRefMixedArgs Expected value is 100, but returned different");
			}
			
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : intStaticRefMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
			
		}		
	
	}
	
	
	public void testMixedTypeMethodsWithString() //1
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"stringMixedArgs");
			Node.createTextElement("string","Cordys Inida Pvt",request);
			Node.createTextElement("xml","<office>Cordys</office>",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<stringMixedArgs><stringMixedArgs>","False");
            String result = Node.getDataWithDefault(
                        new XPath(".//stringMixedArgs/stringMixedArgs").firstMatch(response, null),
                        "Not Found");
			if (!result.equals("Cordys Inida Pvt"))
			{
				fail("stringMixedArgs Expected value is Cordys Inida Pvt, but returned " +result);
			}
			
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- stringMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
			
		}
	}
	
	public void testMixedTypeStaticMethodsWithString() //2
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"stringStaticMixedArgs");
			Node.createTextElement("string","Cordys Inida Pvt",request);
			Node.createTextElement("xml","<office>Cordys</office>",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<stringStaticMixedArgs><stringStaticMixedArgs>","False");
            String result = Node.getDataWithDefault(
                    new XPath(".//stringStaticMixedArgs/stringStaticMixedArgs").firstMatch(response, null),
                    "Not Found");
			if (!result.equals("Cordys Inida Pvt"))
			{
				fail("stringStaticMixedArgs Expected value is Cordys Inida Pvt, but returned "+result);
			}
			
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- stringStaticMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
			
		}

	}
	
	
	public void testMixedTypeRefMethodsWithString() //3
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"stringRefMixedArgs");
			Node.createTextElement("string","Cordys",request);
			Node.createTextElement("xml","<office>Cordys India</office>",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<stringRefMixedArgs><stringRefMixedArgs>","False");
            String result = Node.getDataWithDefault(
                    new XPath(".//stringRefMixedArgs/stringRefMixedArgs").firstMatch(response, null),
                    "Not Found");
			if (!result.equals("Cordys"))
			{
				fail("intRefMixedArgs Expected value is Cordys, but returned different");
			}
			
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : stringRefMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
			
		}		
		
	}
	public void testMixedTypeRefStaticMethodsWithString() //4
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"stringStaticRefMixedArgs");
			Node.createTextElement("string","Cordys",request);
			Node.createTextElement("xml","<office>Cordys India</office>",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<stringStaticRefMixedArgs><stringStaticRefMixedArgs>","False");
            String result = Node.getDataWithDefault(
                    new XPath(".//stringStaticRefMixedArgs/stringStaticRefMixedArgs").firstMatch(response, null),
                    "Not Found");
			if (!result.equals("Cordys"))
			{
				fail("stringStaticRefMixedArgs Expected value is Cordys, but returned " +result);
			}
				
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : stringStaticRefMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
			
		}		
	
	}
	

	
	
	public void testvoidStaticRefMixedArgs() //5
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(namespace,"voidStaticRefMixedArgs");
			Node.createTextElement("string","Cordys",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<old><voidStaticRefMixedArgs>","False");
            String result = Node.getDataWithDefault(
                    new XPath(".//old/voidStaticRefMixedArgs").firstMatch(response, null),
                    "Not Found");
			if (!result.equals("Cordys"))
			{
				fail("stringStaticRefMixedArgs Expected value is Cordys, but returned " +result);
			}
			
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : voidStaticRefMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
		}	
	}
	
	public void testTupable() //6
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(unitNamespace,"testTupable");
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<testTupableResponse>","False");
            String result = Node.getDataWithDefault(
                    new XPath(".//testTupableResponse").firstMatch(response, null),
                    "Not Found");
			if (!result.equals("HyderabadNL"))
			{
				fail("stringStaticRefMixedArgs Expected value is Cordys, but returned " +result);
			}
			
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : voidStaticRefMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
		}	

	}
	
	public void testDefaultRefArgs()
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(unitNamespace,"defaultByRefArgs");
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<defaultByRefArgs><defaultByRefArgs>","False");
            String result = Node.getDataWithDefault(
                    new XPath(".//defaultByRefArgs/defaultByRefArgs").firstMatch(response, null),
                    "Not Found");
			if (!result.equals("Cordys India"))
			{
				fail("testDefaultArgs Expected value is Cordys India, but returned " +result);
			}
			//String refVal = Find.firstDataWithDefault(response,"?<defaultByRefArgs><intVal>","False");
            String refVal = Node.getDataWithDefault(
                    new XPath(".//defaultByRefArgs/intVal").firstMatch(response, null),
                    "Not Found");
			if (!refVal.equals("Cordys India"))
			{
				fail("testDefaultArgs Expected value is Cordys India, but returned " +refVal);
			}
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : voidStaticRefMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
		}	
	}
	

	public void testDefaultrgs()
	{
		int request =0,response =0;
		try
		{
			request 	= connector.createSOAPMethod(unitNamespace,"defaultByValArgs");
			Node.createTextElement("intVal","100",request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
			//String result = Find.firstDataWithDefault(response,"?<defaultByValArgs><defaultByValArgs>","False");
            String result = Node.getDataWithDefault(
                    new XPath(".//defaultByValArgs/defaultByValArgs").firstMatch(response, null),
                    "Not Found");
			if (!result.equals("110"))
			{
				fail("testDefaultrgs Expected value is 110, but returned " +result);
			}
			//String refVal = Find.firstDataWithDefault(response,"?<defaultByRefArgs><intVal>","False");
            String refVal = Node.getDataWithDefault(
                        new XPath(".//defaultByRefArgs/intVal").firstMatch(response, null),
                        "Not Found");
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : voidStaticRefMixedArgs" );
		}
		finally
		{
			Util.delete(Node.getRoot(request));
			Util.delete(Node.getRoot(response));
		}	
	}



}
